<template>
    <h1>添加理财项目信息</h1>

    <table class="table table-bordered">
        <tr>
            <td>理财名称</td>
            <td>
                <input type="text" v-model="Finan.F_FName" >
            </td>
        </tr>

        <tr>
            <td>年收益</td>
            <td>
                <input type="text" v-model="Finan.F_YearMoney" >
            </td>
        </tr>

        <tr>
            <td>最低金额</td>
            <td>
                <input type="text" v-model="Finan.F_MinMoney" >
            </td>
        </tr>

        <tr>
            <td>理财风险等级</td>
            <td>
                <input type="radio" name="Flevel" value="0" v-model="Finan.F_FinanLevel" checked >低风险 
                <input type="radio" name="Flevel" value="1" v-model="Finan.F_FinanLevel"  >中风险
                <input type="radio" name="Flevel" value="2" v-model="Finan.F_FinanLevel"  >高风险
            </td>
        </tr>

        <tr>
            <td>理财利率</td>
            <td>
                <select v-model="Finan.I_ID" >
                    <option value="0">--请选择--</option>
                    <option v-for="item in Interest" :key="item.I_ID" :value="item.I_ID">
                        {{ item.I_Year }} 年利率 {{ item.I_Interest }}%
                    </option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="保存" class="btn btn-success" @click="AddFinanInfo" >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

//用于添加的响应式数据
const Finan = ref({
    F_FName:'',
    F_YearMoney:'',
    F_MinMoney:'',
    F_FinanLevel:0,
    I_ID:0,
})

//用于绑定的响应式数据
const Interest = ref([{
    I_ID:"",
    I_Year:"",
    I_Interest:""
}])

//生命周期钩子函数
onMounted(()=>{
    ShowInterestInfo()
})

//绑定方法
const ShowInterestInfo = ()=>{
    axios({
        method:'get',
        url:'http://localhost:63517/api/Finan/ShowInterestInfo',
    })
    .then(res=>{
        console.log(res.data);
        Interest.value = res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

const AddFinanInfo = ()=>{

    if(Finan.value.F_FName==''){
        alert("理财名称不能为空");
        return;
    }

    if( Number(Finan.value.F_MinMoney)<1 || Number(Finan.value.F_MinMoney)>100000000 ){
        alert("理财金额大于0且小于1亿元");
        return;
    }

    axios({
        method:'post',
        url:'http://localhost:63517/api/Finan/AddFinanInfo',
        data:{
            F_FName:Finan.value.F_FName,
            F_YearMoney:Finan.value.F_YearMoney,
            F_MinMoney:Finan.value.F_MinMoney,
            F_FinanLevel:Finan.value.F_FinanLevel,
            I_ID:Finan.value.I_ID,
        },
    })
    .then(res=>{
        if(res.data>0){
            alert("添加成功");
            router.push('/ShowFinan');
        }
        else if(res.data==-1){
            alert("理财产品名称重复");
            return;
        }
        else{
            alert("添加失败");
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

</script>
